//
//  AboutViewController.m
//  BosidengIphone
//
//  Created by  on 11-12-29.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "AboutViewController.h"
#import "SyncImageView.h"
#import "MainViewController.h"
#import "AppDelegate.h"

@implementation AboutViewController
@synthesize mData;
@synthesize title;
@synthesize scrollView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc {
    [categoryView release];
    [mData release];
    [title release];
    [scrollView release];
    [super dealloc];
}

#pragma mark - View lifecycle

- (void)initLogoView {
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    categoryView = [[AboutCategoryViewController alloc] initWithNibName:@"AboutCategoryViewController" bundle:nil];
    NSArray *array = [BrandData getAllItems];
    for (BrandData *categoryData in array) {
        if ([@"1" isEqualToString:categoryData.brandID]) {
            categoryData.name = [AppDelegate getLanguageString:@"about_category_all"];
        }
    }
    categoryView.brands = array;
    categoryView.delegate = self;
    categoryView.view.frame = CGRectMake(0, -392, 320, 392);
    
    if (mData == nil) {
        return;
    }
    title.text = mData.name;
    
    NSString *logoUrl = mData.logo;
    SyncImageView *imageView = [[SyncImageView alloc] initWithUrl:logoUrl andRequestQueue:requestQueue];
    imageView.frame = CGRectMake(98, 34, 124, 64);
    [self.view addSubview:imageView];
    [imageView release];
    
    int height = 0;
    int contentHeight = [mData getContentHeight];
    UILabel *contentView = [[UILabel alloc] init];
    contentView.font = [mData getFont];
    contentView.text = mData.introduce;
    contentView.numberOfLines = [mData getLineNumbers];
    contentView.frame = CGRectMake(10, 0, 300, contentHeight);
    [scrollView addSubview:contentView];
    [contentView release];
    height += contentHeight;
    
    if ([mData hasBrandImgs]) {
        NSArray *images = [mData.iPhoneImages componentsSeparatedByString: @"|"];
        for (NSString *imgInfo in images) {
            NSArray *infos = [imgInfo componentsSeparatedByString:@","];
            NSString *url = [infos objectAtIndex:0];
            NSInteger imgHeight = [[infos objectAtIndex:1] intValue] / 2;
            SyncImageView *imageView = [[SyncImageView alloc] initWithUrl:url andRequestQueue:requestQueue];
            imageView.frame = CGRectMake(0, height, 320, imgHeight);
            [scrollView addSubview:imageView];
            [imageView release];
            height += imgHeight;
        }
    }
    
    scrollView.contentSize = CGSizeMake(320, height);
    
    [self.view addSubview:categoryView.view];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)showMenuAnimation:(UIView*)view {
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:1];
	[UIView setAnimationDelay:0.0];
    CGAffineTransform transform = CGAffineTransformMakeTranslation(0, 416);
    view.transform = transform;
	[UIView commitAnimations];
}

- (IBAction)selectBrands:(id)sender {
    [self showMenuAnimation:categoryView.view];
}

- (void)changeViewController:(BrandData *)data {
    AboutViewController *viewController = [[AboutViewController alloc] initWithNibName:@"AboutViewController" bundle:nil];
    viewController.mData = data;
    [(MainViewController *)mainViewController changeTopViewController:viewController];
    [viewController release];
}

@end
